﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HenIT.Data.SqlClient;
using System.Data.SqlClient;
using System.Data;
namespace EventScavenger
{
	public partial class EventScavengerDAL : GenericSQLServerDAL
	{
        internal void DeleteMachineLog(int machineLogId)
        {
            string sql = "DeleteMachineLog";
            SqlParameter[] parameters = new SqlParameter[] 
				{
					new SqlParameter("@MachineLogID", machineLogId)
				};
            ExecuteSP(sql, parameters);
        }
        internal void UpdateMachineLogEntry(MachineLogEntry mle, bool throwError = false)
		{
            string sql = "UpdateMachineLogEntry";
            try
            {
                object pollfilterIdobj = null;
                if (mle.PollFilterID > 0)
                    pollfilterIdobj = mle.PollFilterID;
                SqlParameter[] parameters = new SqlParameter[] 
				{
					new SqlParameter("@CollectorID", mle.CollectorId),
					new SqlParameter("@Machine", mle.Machine),
					new SqlParameter("@LogName", mle.LogName),
					new SqlParameter("@LastPollTime", mle.LastPollTime),
					new SqlParameter("@Enabled", mle.Enabled),
					new SqlParameter("@PollFrequency", mle.PollFrequency),
					new SqlParameter("@MaxEntryCount", mle.MaxEntryCount),
					new SqlParameter("@PollFilterID", pollfilterIdobj),
                    new SqlParameter("@DBImportEnabled", mle.DBImportEnabled),
                    new SqlParameter("@DBImportServer", mle.DBImportServer),
                    new SqlParameter("@DBImportDatabase", mle.DBImportDatabase),
				};
                ExecuteSP(sql, parameters);
            }
            catch 
            {
                if (throwError)
                    throw;
            }
		}
		internal List<MachineLogEntry> GetMachineLogList(bool hideDisdabled)
		{
			List<MachineLogEntry> list = new List<MachineLogEntry>();
			string sql = "SelectMachineLogList2";

			SqlParameter[] parameters = new SqlParameter[] 
				{
					new SqlParameter("@hideDisabled", hideDisdabled),
				};
			DataSet mleds = GetDataSet(sql, CommandType.StoredProcedure, parameters);
			if (mleds.Tables.Count > 0 && mleds.Tables[0].Rows.Count > 0)
			{
				foreach (DataRow dr in mleds.Tables[0].Rows)
				{
					MachineLogEntry mle = new MachineLogEntry(dr["Machine"].ToString(), dr["LogName"].ToString());
					mle.MachineLogID = (int)dr["MachineLogID"];
					mle.CollectorId = (int)dr["CollectorID"];
					mle.Collector = dr["Collector"].ToString();
					mle.Enabled = (bool)dr["Enabled"];
					mle.PollFrequency = (int)dr["PollFrequency"];
					mle.MaxEntryCount = (int)dr["MaxEntryCount"];
					if (dr["LastPollTime"] != DBNull.Value)
						mle.LastPollTime = (DateTime)dr["LastPollTime"];
					if (dr["Oldest Entry"] != DBNull.Value)
					{
						DateTime t;
						if (DateTime.TryParse(dr["Oldest Entry"].ToString(), out t))
							mle.OldestEntry = t;
					}
					if (dr["Newest Entry"] != DBNull.Value)
					{
						DateTime t;
						if (DateTime.TryParse(dr["Newest Entry"].ToString(), out t))
							mle.NewestEntry = t;
					}
					int tint = 0;
					if (int.TryParse(dr["Entry Count"].ToString(), out tint))
						mle.EntryCount = tint;
					if (dr["PollFilterID"] != DBNull.Value)
						mle.PollFilterID = (int)dr["PollFilterID"];
                    mle.DBImportEnabled = (bool)dr["DBImportEnabled"];
                    mle.DBImportServer = N(dr["DBImportServer"]);
                    mle.DBImportDatabase = N(dr["DBImportDatabase"]);
					list.Add(mle);
				}
			}
			return list;
		}
		internal List<MachineLogEntry> GetMachineLogListAll()
		{
			List<MachineLogEntry> list = new List<MachineLogEntry>();
			string sql = "SelectMachineLogListAll";
			DataSet mleds = GetDataSet(sql, CommandType.StoredProcedure);
			if (mleds.Tables.Count > 0 && mleds.Tables[0].Rows.Count > 0)
			{
				foreach (DataRow dr in mleds.Tables[0].Rows)
				{
					MachineLogEntry mle = new MachineLogEntry(dr["Machine"].ToString(), dr["LogName"].ToString());
					mle.MachineLogID = (int)dr["MachineLogID"];
					mle.CollectorId = (int)dr["CollectorID"];
					mle.Collector = dr["Collector"].ToString();
					mle.Enabled = (bool)dr["Enabled"];
					mle.PollFrequency = (int)dr["PollFrequency"];
					mle.MaxEntryCount = (int)dr["MaxEntryCount"];
					if (dr["LastPollTime"] != DBNull.Value)
						mle.LastPollTime = (DateTime)dr["LastPollTime"];
					if (dr["PollFilterID"] != DBNull.Value)
						mle.PollFilterID = (int)dr["PollFilterID"];
                    mle.DBImportEnabled = (bool)dr["DBImportEnabled"];
                    mle.DBImportServer = N(dr["DBImportServer"]);
                    mle.DBImportDatabase = N(dr["DBImportDatabase"]);
					list.Add(mle);
				}
			}
			return list;
		}
        internal List<Tuple<string, string>> GetExistingDBImportNames()
        {
            List<Tuple<string, string>> list = new List<Tuple<string, string>>();
            string sql = "SelectExistingDBImportNames";
            DataSet dblist = GetDataSet(sql, CommandType.StoredProcedure);
            if (dblist.Tables.Count > 0 && dblist.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in dblist.Tables[0].Rows)
                {
                    Tuple<string, string> entry = new Tuple<string, string>(dr[0].ToString(), dr[1].ToString());
                    list.Add(entry);
                }
            }
            return list;
        }
        internal void ClearAllEntriesForMachineLog(int machineLogId)
        {
            string sql = "ClearAllEntriesForMachineLog";
            SqlParameter[] parameters = new SqlParameter[] 
				{
					new SqlParameter("@MachineLogID", machineLogId)
				};
            ExecuteSP(sql, parameters);
        }
	}
}
